//
//  ByViewDetailSkuInfo.m
//  oc_knowledge
//
//  Created by pro on 17/1/18.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewDetailSkuInfo.h"

@implementation ByViewDetailSkuInfo
{
    UIView      *_bgView;           // 背景
    UIView      *_line;             // 线条
    UIImageView *_imgView;          // 主图
    UIButton    *_closeBtn;         // 关闭
    UILabel     *_priceLabel;       // 价格
    UILabel     *_storeLabel;       // 库存
    UILabel     *_skuDescLabel;     // 规格描述
    
}
- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, self.width, self.height - 10)];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    
    _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, self.height - 10, self.height - 10)];
    [self addSubview:_imgView];
    
    _line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 1)];
    _line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [_bgView addSubview:_line];
    _line.bottom = _bgView.height;
    
    _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _closeBtn.frame = CGRectMake(0, 0, 20, 20);
    [_closeBtn setImage:[UIImage imageNamed:@"product_detail_close"] forState:UIControlStateNormal];
    [_closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [_bgView addSubview:_closeBtn];
    _closeBtn.right = _bgView.width - 10;
    _closeBtn.top = 10;
    
    _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(_imgView.right + 10, 0, _bgView.width - (_imgView.right + 10), 20)];
    _priceLabel.adjustsFontSizeToFitWidth = YES;
    _priceLabel.font = FONT(18);
    _priceLabel.text = @"￥666-888";
    _priceLabel.textColor = [UIColor orangeColor];
    _priceLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_priceLabel];
    
    _storeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_priceLabel.left, 0, _priceLabel.width, _priceLabel.height)];
    _storeLabel.adjustsFontSizeToFitWidth = YES;
    _storeLabel.font = FONT(14);
    _storeLabel.text = @"库存1212件";
    _storeLabel.textColor = [UIColor blackColor];
    _storeLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_storeLabel];
    
    _skuDescLabel = [[UILabel alloc] initWithFrame:CGRectMake(_storeLabel.left, 0, _storeLabel.width, _storeLabel.height)];
    _skuDescLabel.adjustsFontSizeToFitWidth = YES;
    _skuDescLabel.font = FONT(14);
    _skuDescLabel.text = @"选择规格分类";
    _skuDescLabel.textColor = [UIColor blackColor];
    _skuDescLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_skuDescLabel];
    
    CGFloat margin = 3;
    CGFloat remain = (_bgView.height - _priceLabel.height - _storeLabel.height - _skuDescLabel.height - margin * 2) / 2;
    _priceLabel.top = remain;
    _storeLabel.top = _priceLabel.bottom + margin;
    _skuDescLabel.top = _storeLabel.bottom + margin;
    
}

- (void)setUnSelectedModel:(ByModelProductDetail *)unSelectedModel {
    
    _unSelectedModel = unSelectedModel;
    
    [self setLabelsAttributes:nil];

}
- (void)setSelectedModel:(SkuModel *)selectedModel {
    
    _selectedModel = selectedModel;
    
    [self setLabelsAttributes:_selectedModel];
}


#pragma mark - 点击
- (void)close {
    
    if (self.closeBlock) {
        self.closeBlock();
    }
}

#pragma mark - 辅助
- (void)setLabelsAttributes:(SkuModel *)model {
    
    if (model) {
        [_imgView setImageWithURL:[NSURL URLWithString:model.icon_url] placeholderImage:DEFAULT_IMAGE_1_2];
        _priceLabel.text = [NSString stringWithFormat:@"￥%.1lf", model.price.floatValue];
        _storeLabel.text = [NSString stringWithFormat:@"库存%@件", model.quantity];
        _skuDescLabel.text = model.sku_desc;
    } else {
        [_imgView setImageWithURL:[NSURL URLWithString:_unSelectedModel.main_img] placeholderImage:DEFAULT_IMAGE_1_2];
        _priceLabel.text = [NSString stringWithFormat:@"￥%.1lf-%.1lf", _unSelectedModel.price.floatValue, _unSelectedModel.oriPrice.floatValue];
        _storeLabel.text = [NSString stringWithFormat:@"库存%@件", _unSelectedModel.quantity];
        _skuDescLabel.text = @"选择规格分类";
    }
}

@end
